﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Fetion.Util
{
    public class MiMaHelper
    {
        private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        public static string ToHexString(byte[] bytes)
        {
            int num = bytes.Length;
            char[] chars = new char[num * 2];
            for (int i = 0; i < num; i++)
            {
                int b = bytes[i];
                chars[i * 2] = hexDigits[b >> 4];
                chars[i * 2 + 1] = hexDigits[b & 0xF];
            }
            return new string(chars);
        }
        public static String createNonceUseRandom()
        {
            Random ra = new Random();
            int n1 = 0;
            int n2 = 0;
            int n3 = 0;
            int n4 = 0;
            n1 = ra.Next();
            n2 = ra.Next();
            n3 = ra.Next();
            n4 = ra.Next();
            if (n1 >> 24 < 16)
            {
                n1 += 0x10000000;
            }
            if (n2 >> 24 < 16)
            {
                n2 += 0x10000000;
            }
            if (n3 >> 24 < 16)
            {
                n3 += 0x10000000;
            }
            if (n4 >> 24 < 16)
            {
                n4 += 0x10000000;
            }
            //string a1 = String.Format("{0:X}", n1);
            //string a2 = String.Format("{0:X}", n2);
            //string a3 = String.Format("{0:X}", n3);
            //string a4 = String.Format("{0:X}", n4);
            return String.Format("{0:X}{1:X}{2:X}{3:X}", n1, n2, n3, n4);
        }


        public static byte[] mergeArrays(byte[] array1, byte[] array2)
        {
            byte[] result = new byte[array1.Length + array2.Length];
            array1.CopyTo(result, 0);
            array2.CopyTo(result, array1.Length);
            return result;
        }
        public static byte[] SHA1(string text)
        {
            var sha = new SHA1Managed();
            var bytes = System.Text.Encoding.UTF8.GetBytes(text);
            byte[] resultHash = sha.ComputeHash(bytes);
            return resultHash;
        }
        public static byte[] SHA1(byte[] buffer)
        {
            var sha = new SHA1Managed();
            byte[] resultHash = sha.ComputeHash(buffer);
            return resultHash;
        }
        public static byte[] getIntegerBytes(int i)
        {
            byte[] bytes = new byte[4];
            bytes[0] = (byte)(0xff & i);
            bytes[1] = (byte)((0xff00 & i) >> 8);
            bytes[2] = (byte)((0xff0000 & i) >> 16);
            bytes[3] = (byte)((0xff000000 & i) >> 24);
            return bytes;
        }
    }
}
